<?php
require "rsa.php";
require "jwt.php";
require "util.php";

header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Methods:*");
header("Access-Control-Allow-Headers:Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,Authorization");


$getMethod = @$_GET['request'];

//routers
$gets = array(
    'getNewsList'=>'getNewsList',
    'getNoticeList'=>'getNoticeList',
    'getStudentList'=>'getStudentList',
    'getArticle'=>'getArticle',

);

if (!empty($getMethod)) {
    echo $gets[$getMethod]();
} else {
    echo 'server is runnning' . $postMethod . $getMethod;
}

//controllers

function getNewsList(){
    $count = @$_GET['count'];//14
    $offset = @$_GET['offset'];//1
    $pdo = getConnection();
    $stmt = $pdo->prepare("select count(*) from articles where part_type = 100 and state = 0 ");
    $stmt->execute();
    $total = $stmt->fetchAll();

    $sql = 'select a_id,title,publish_time from articles where part_type = 100 and state = 0 order by publish_time desc limit '.$count*$offset." , ".$count;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $res = $stmt->fetchAll();
    return_result(array('code'=>200,'data'=>array('total'=>$total,"list"=>$res),'msg'=>'success'));
}
function getNoticeList(){
    $count = @$_GET['count'];//14
    $offset = @$_GET['offset'];//1
    $pdo = getConnection();
    $stmt = $pdo->prepare("select count(*) from articles where part_type = 101 and state = 0");
    $stmt->execute();
    $total = $stmt->fetchAll();

    $sql = 'select a_id,title,publish_time from articles where part_type = 101 and state = 0 order by publish_time desc limit '.$count*$offset." , ".$count;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $res = $stmt->fetchAll();
    return_result(array('code'=>200,'data'=>array('total'=>$total,"list"=>$res),'msg'=>'success'));
}
function getStudentList(){
    $count = @$_GET['count'];//14
    $offset = @$_GET['offset'];//1
    $pdo = getConnection();
    $stmt = $pdo->prepare("select count(*) from articles where part_type = 102 and state = 0");
    $stmt->execute();
    $total = $stmt->fetchAll();

    $sql = 'select a_id,title,publish_time from articles where part_type = 102 and state = 0 order by publish_time desc limit '.$count*$offset." , ".$count;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $res = $stmt->fetchAll();
    return_result(array('code'=>200,'data'=>array('total'=>$total,"list"=>$res),'msg'=>'success'));
}
function getArticle(){
    $id = @$_GET['id'];
    $pdo = getConnection();

    $sql = "select view_count from articles where a_id = ".$id;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $count = $stmt->fetchAll();
    $count = $count[0][0];

    $sql = 'update articles set view_count = '.($count+1).' where a_id = '.$id;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    $sql = 'select * from articles where state = 0 and a_id = '.$id;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $res = $stmt->fetchAll();
    $item = $res[0];
    $raw = $item['content'];
    $result = explode("'",$raw)[1];
    $item['content'] = base64_decode($result);
    return_result(array('code'=>200,'data'=>array("list"=>$item),'msg'=>'success'));
}